package model;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class Room extends PersistentEntity implements Comparable<Room> {
  private String name;
  private List<Round> rounds;
  private Set<User> users;
  private Question question;
  private int maxUsers;
  private int expirationTime;

  public Room(String name, Question question, int maxUsers, int expirationTime) {
    super();
    this.setName(name);
    this.setQuestion(question);
    this.setMaxUsers(maxUsers);
    this.users = new TreeSet<User>();
    this.rounds = new ArrayList<Round>();
    this.expirationTime = expirationTime;

  }

  public void setName(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void addRound(Round r) {
    this.rounds.add(r);
  }

  public List<Round> getRounds() {
    return rounds;
  }

  public void addUser(User u) {
    if(users.size()< this.maxUsers){
      //no importa si el usuario ya esta en la room
        this.users.add(u);
    } else {
      throw new RuntimeException("The room is full");
    }
  }

  public Set<User> getUsers() {
    return users;
  }

  public void setQuestion(Question question) {
    this.question = question;
  }

  public Question getQuestion() {
    return question;
  }

  public void setMaxUsers(int maxUsers) {
    this.maxUsers = maxUsers;
  }

  public int getMaxUsers() {
    return maxUsers;
  }

  public Round getLastRound() {
    int size = rounds.size();
    if (size == 0) {
      throw new RuntimeException("There are no rounds yet");
    }
    return rounds.get(size - 1);
  }

  public int getQUsers() {
    return this.users.size();
  }

  public int getExpirationTime() {
    return this.expirationTime;
  }

  @Override
  public int compareTo(Room o) {
    return this.name.compareTo(o.name);
  }

  public Boolean hasActiveRound() {
    if (this.rounds.size() == 0 || this.getLastRound().isClosed()) {
      return false;
    } else {
      return true;
    }
  }

}
